package com.vdg.fps;

import com.vdg.R;
import com.vdg.fps.data.FaceBookMessage;
import com.vdg.fps.ulti.Utilities;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;

public class MessageActivity extends Activity {
	private Button mBtnClear;
	private Button mBtnSend;
	private EditText mEdtMessage;
	private EditText mEdtTag;
	private EditText mEdtNumIds;
	private CheckBox mChbRandomIds;
	private CheckBox mChbCustomThumbnail;
	private EditText mEdtLink;
	private EditText mEdtTitle;
	private EditText mEdtDesc;
	private EditText mEdtImg;
	public static final String MESSAGE_KEY = "message";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.message_layout);
		initView();
	}

	public void initView() {
		mBtnClear = (Button) findViewById(R.id.btn_clear);
		mBtnSend = (Button) findViewById(R.id.btn_send);
		mEdtMessage = (EditText) findViewById(R.id.edt_message);
		mEdtTag = (EditText) findViewById(R.id.edt_tag);
		mEdtNumIds = (EditText) findViewById(R.id.edt_num_ids);
		mChbRandomIds = (CheckBox) findViewById(R.id.chb_random_ids);
		mChbCustomThumbnail = (CheckBox) findViewById(R.id.chb_custom_thumbnail);
		mEdtLink = (EditText) findViewById(R.id.edt_link);
		mEdtTitle = (EditText) findViewById(R.id.edt_title);
		mEdtDesc = (EditText) findViewById(R.id.edt_desc);
		mEdtImg = (EditText) findViewById(R.id.edt_image);
		mBtnSend.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				actionSend();
			}
		});
		mChbCustomThumbnail
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(CompoundButton buttonView,
							boolean isChecked) {
						// TODO Auto-generated method stub
						if (!isChecked) {
							mEdtLink.setEnabled(false);
							mEdtTitle.setEnabled(false);
							mEdtDesc.setEnabled(false);
							mEdtImg.setEnabled(false);
						} else {
							mEdtLink.setEnabled(true);
							mEdtTitle.setEnabled(true);
							mEdtDesc.setEnabled(true);
							mEdtImg.setEnabled(true);
							mEdtLink.requestFocus();
						}
					}
				});
		mChbRandomIds.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				// TODO Auto-generated method stub
				if (isChecked) {
					mEdtNumIds.setEnabled(true);
					mEdtNumIds.requestFocus();
				} else {
					mEdtNumIds.setEnabled(false);
				}
			}
		});
		mEdtLink.setEnabled(false);
		mEdtTitle.setEnabled(false);
		mEdtDesc.setEnabled(false);
		mEdtImg.setEnabled(false);
		mEdtNumIds.setEnabled(false);
		mBtnClear.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				mEdtMessage.setText("");
				mEdtTag.setText("");
				mEdtNumIds.setText("1");
				mChbRandomIds.setChecked(false);
				mChbCustomThumbnail.setChecked(false);
				mEdtLink.setText("");
				mEdtTitle.setText("");
				mEdtDesc.setText("");
				mEdtImg.setText("");
			}
		});
	}

	private void actionSend() {
		if (mEdtMessage.getText().toString().isEmpty()) {
			Utilities.showToast("The message is required field!", this);
			mEdtMessage.requestFocus();
			return;
		}
		if (mChbCustomThumbnail.isChecked()) {
			if (mEdtLink.getText().toString().isEmpty()) {
				Utilities.showToast("The Link is required field!", this);
				mEdtLink.requestFocus();
				return;
			}
			if (mEdtTitle.getText().toString().isEmpty()) {
				Utilities.showToast("The Title is required field!", this);
				mEdtTitle.requestFocus();
				return;
			}
			if (mEdtDesc.getText().toString().isEmpty()) {
				Utilities.showToast("The Description is required field!", this);
				mEdtDesc.requestFocus();
				return;
			}
			if (mEdtImg.getText().toString().isEmpty()) {
				Utilities.showToast("The Image is required field!", this);
				mEdtImg.requestFocus();
				return;
			}
		}
		FaceBookMessage msg = new FaceBookMessage();
		msg.setText(mEdtMessage.getText().toString().trim());
		msg.setFrTag(mEdtTag.getText().toString().trim());
		msg.setRandFrTag(mChbRandomIds.isChecked());
		msg.setRandFrTagCount(Integer.parseInt(mEdtNumIds.getText().toString()));
		msg.setType(0);
		if (mChbCustomThumbnail.isChecked()) {
			msg.setLink(mEdtLink.getText().toString().trim());
			msg.setTitle(mEdtTitle.getText().toString().trim());
			msg.setDesc(mEdtDesc.getText().toString().trim());
			msg.setImg(mEdtImg.getText().toString().trim());
		}
		Intent intent = new Intent();
		intent.putExtra(MESSAGE_KEY, msg);
		setResult(RESULT_OK, intent);
		finish();
	}
}
